<?php
/**
 * @author Feng
 * @date 2012-8-5
 */
class controller_admin_resource_friendsite extends system_user_controller_panel {
	
	private $concept = null;
	public function init() {
		if(!parent::init()) {
			return ;
		}
		$this->concept = new resource_friendsite_concept($this->user->getLang());
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Resource'))->setRoute('resource/index'));
		$this->_crumbs()->addPage(system_nav_page::create($this->language('Friend Sites'))->setRoute('resource/friendsite/index'));
	}
	
	public function action_addnew() {
		$row = $this->concept->create();
		$this->view->row = $row;
		$row->setUrl('http://');
		$row->setIsActive(1);
		$row->setDisplayOrder(0);
		$row = $this->_saveRow($row);
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($this->language('New Friend Site'))->setRoute('resource/friendsite/addnew'));
		$this->render('resource/friendsite/row.phtml');
	}
	
	public function action_edit() {
		$id = intval($this->_param('id'));
		if(!($row = $this->concept->fetch(array('friendsite.id = ?' => $id)))) {
			throw new collection_exception("The Site is not Existing.");
		}
		$row = $this->_saveRow($row);
		$this->view->row = $row;
		$this->view->referer = $this->referer;
		$this->_crumbs()->addPage(system_nav_page::create($row->getName())->setRoute('resource/friendsite/edit')->setParams(array('id' => $row->getId())));
		$this->render('resource/friendsite/row.phtml');
	}
	
	private function _saveRow($row) {
		if(!$this->getRequest()->isPost()) {
			return $row;
		}
		$this->referer = $this->_post('referer');
		$row->setName(trim($this->_post('name')));
		$row->setUrl(trim($this->_post('url')));
		$row->setDisplayOrder(intval($this->_post('display_order')));
		$row->setDescription($this->_post('description'));
		$row->setIsActive(intval($this->_post('is_active')) ? 1 : 0);		
		$this->_saveToDatabase($row, 'logo', 'image', false);		
		return $row;
	}
		
	public function action_index() {
		$this->_goRoute('resource/friendsite/list');
		$this->render();
	}
	
	public function action_list() {	
		$this->_batchDelete($this->concept, 'id', 'id');
		if($this->getRequest()->isPost() && $this->_post('saveChange')) {
			try {
				$_dorder = (array) $this->_post('_display_order');
				foreach((array)$this->_post('display_order') AS $id => $value) {
					if(isset($_dorder[$id]) && $_dorder[$id] == $value) continue;
					system_db::getAdapter()->query("UPDATE `resource_friendsite` SET `display_order` = '$value' WHERE `id` = '$id'");
				}
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}	
		$adapter = new system_paginator_adapter_concept($this->concept);
		$adapter->setCurrentPageNumber(intval($this->_param('page', 1)));
		
		$this->view->order = $order = strtolower($this->_get('order'));
		$keyword = trim($this->_get('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$adapter->where("`name` LIKE ? OR link.`url` LIKE ?", "%$keyword%");
		}
		$_order = array("display_order ASC");
		if($order) {
			$this->view->sort = $sort = intval($this->_get('sort'));
			switch($order) {
				case 'date': {
					$order = 'creationtime';
					break;
				}
				case 'click': {
					$order = 'link.click';
					break;
				}
				case 'order': default: {
					$order = 'display_order';
					break;
				}
			}
			$_sort = !$sort ? 'DESC' : 'ASC';
			$_order = array("$order $_sort");
		}
		$adapter->setLimit(30);
		$adapter->order($_order);
		$this->view->list = new system_paginator($adapter);
		$this->_crumbs()->addPage(system_nav_page::create($this->language('List'))->setRoute('resource/friendsite/list'));
		$this->render();
	}
}
